﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cua_Hang.DTO;
using System.Data;

namespace Cua_Hang.DAO
{
    public class ToaDo_KhachHang_DAO
    {
        //==================================================================

        //-------load toa do khach hang------------
        public static List<ToaDo_KhachHang_DTO> List_ToaDoKhachHang_DAO(int ID_cuahang)
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM Cua_Hang AS T1 JOIN ToaDo_KhachHang AS T2 ON T1.ID_cuahang=T2.ID_cuahang WHERE T2.ID_cuahang like '%" + ID_cuahang + "%'";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<ToaDo_KhachHang_DTO> lstToaDo_KhachHang = new List<ToaDo_KhachHang_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    ToaDo_KhachHang_DTO dm = new ToaDo_KhachHang_DTO();
                    dm.ID_ToaDo = (int)dr["ID_ToaDo"];
                    dm.ID_cuahang = (int)dr["ID_cuahang"];
                    dm.ToaDo_X = dr["ToaDo_X"].ToString();
                    dm.ToaDo_Y = dr["ToaDo_Y"].ToString();


                    lstToaDo_KhachHang.Add(dm);
                }
                return lstToaDo_KhachHang;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc load toa do khach hang------------

        //==================================================================
        //==================================================================

        //-------load all toa do khach hang------------
        public static List<ToaDo_KhachHang_DTO> List_All_ToaDoKhachHang_DAO()
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM Cua_Hang AS T1 JOIN ToaDo_KhachHang AS T2 ON T1.ID_cuahang=T2.ID_cuahang";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<ToaDo_KhachHang_DTO> lstToaDo_KhachHang = new List<ToaDo_KhachHang_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    ToaDo_KhachHang_DTO dm = new ToaDo_KhachHang_DTO();
                    dm.ID_ToaDo = (int)dr["ID_ToaDo"];
                    dm.ID_cuahang = (int)dr["ID_cuahang"];
                    dm.ToaDo_X = dr["ToaDo_X"].ToString();
                    dm.ToaDo_Y = dr["ToaDo_Y"].ToString();


                    lstToaDo_KhachHang.Add(dm);
                }
                return lstToaDo_KhachHang;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc load all toa do khach hang------------

        //=============================================================
        //==================================================================
        //-----------them toa do cua hang ------------

        public static void Them_ToaDo_CuaHang_DAO(ToaDo_KhachHang_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "INSERT INTO ToaDo_KhachHang(ID_cuahang,ToaDo_X,ToaDo_Y) VALUES(";
                //strSQL += "N'" + kh.Ten_KhachHang + "',";
                //strSQL += "N'" + kh.DiaChi_KhachHang + "',";
                //strSQL += "N'" + kh.Email_KhachHang + "',";
                strSQL += "N'" + ch.ID_cuahang + "',";
                strSQL += "N'" + ch.ToaDo_X + "',";
                strSQL += "N'" + ch.ToaDo_Y + "')";
                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }


        //-------ket thuc them toa do cua hang  ------------
        //==============================================================
        //=============================================================
        //------ xoa toa do cua hang ------------
        public static void Xoa_ToaDo_CuaHang_DAO(ToaDo_KhachHang_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "DELETE FROM ToaDo_KhachHang WHERE ID_ToaDo ='" + ch.ID_ToaDo + "'";

                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc xoa toa do cua hang------------

        //==============================================================
        //==============================================================
        //------ sua chi tiet cua hang  ------------
        public static void Sua_ToaDoCuaHang_DAO(ToaDo_KhachHang_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "UPDATE ToaDo_KhachHang SET ToaDo_X = N'" + ch.ToaDo_X + "',ToaDo_Y=N'" + ch.ToaDo_Y + "' WHERE ID_ToaDo = '" + ch.ID_ToaDo + "'";
                //string strSQL = "UPDATE ChiTiet_CuaHang " +
                //"SET Ten_cuahang = '" + ch.Ten_cuahang +

                //" DiaChi = '" + info.DiaChi + "', " +
                //" NgaySinh = '" + info.NgaySinh.ToShortDateString() + "', " +
                //" MaLop = '" + info.MaLop + "'," +
                //" GioiTinh = '" + info.GioiTinh + "', " +
                //" NamHoc = " + info.NamHoc +
                // " WHERE ID_cuahang = '" + ch.ID_cuahang + "'";

                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc sua chi tiet cua hang  ------------
        //==============================================================
        //==============================================================
        //------ ham kiem tra toa do cua hang co trong csdl hay chua ------------
        public static Boolean KiemTraTrung_ToaDo_CuaHang_DAO(ToaDo_KhachHang_DTO ch)
        {
            Boolean kq = false;
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM ToaDo_KhachHang  WHERE  ToaDo_X =N'" + ch.ToaDo_X + "' AND ToaDo_Y =N'" + ch.ToaDo_Y + "'";
                DataTable dt = d.ExecuteQuery(strSQL);
                if (dt.Rows.Count > 0)
                {
                    kq = true;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
            return kq;
        }
        //-------ket thuc ham kiem tra toa do cua hang co trong csdl hay chua -----------

        //==============================================================
        //==================================================================

        //-------load toa do khach hang------------
        public static List<ToaDo_KhachHang_DTO> List_ToaDoKhachHangTheoID_DAO(int ID_ToaDo)
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM Cua_Hang AS T1 JOIN ToaDo_KhachHang AS T2 ON T1.ID_cuahang=T2.ID_cuahang WHERE T2.ID_ToaDo like '%" + ID_ToaDo + "%'";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<ToaDo_KhachHang_DTO> lstToaDo_KhachHang = new List<ToaDo_KhachHang_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    ToaDo_KhachHang_DTO dm = new ToaDo_KhachHang_DTO();
                    dm.ID_ToaDo = (int)dr["ID_ToaDo"];
                    dm.ID_cuahang = (int)dr["ID_cuahang"];
                    dm.ToaDo_X = dr["ToaDo_X"].ToString();
                    dm.ToaDo_Y = dr["ToaDo_Y"].ToString();


                    lstToaDo_KhachHang.Add(dm);
                }
                return lstToaDo_KhachHang;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc load toa do khach hang------------



    }
}